package com.example.nep.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.nep.dto.AdminPageDTO;
import com.example.nep.dto.AqiFeedBackAddDTO;
import com.example.nep.dto.AqiPageDTO;
import com.example.nep.model.AqiFeedback;
import com.example.nep.dao.AqiFeedbackMapper;
import com.example.nep.model.Statistics;
import com.example.nep.service.IAqiFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.nep.service.IStatisticsService;
import com.example.nep.vo.*;
import com.sun.org.glassfish.external.statistics.Statistic;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2024-04-10
 */
@Service
public class AqiFeedbackServiceImpl extends ServiceImpl<AqiFeedbackMapper, AqiFeedback> implements IAqiFeedbackService {
    public static final Integer LOCKED=new Integer(0);
    @Resource
    private IStatisticsService statisticsService;
    @Resource
    private AqiFeedbackMapper aqiFeedbackMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addFeedBack(AqiFeedBackAddDTO feedBackAddDTO) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        String[] s = format.split(" ");
        String afDate=s[0];
        String afTime=s[1];
        AqiFeedback aqiFeedback = new AqiFeedback();
        BeanUtils.copyProperties(feedBackAddDTO,aqiFeedback);
        aqiFeedback.setAfDate(afDate);
        aqiFeedback.setAfTime(afTime);
        aqiFeedback.setInformation(feedBackAddDTO.getInformation());
        aqiFeedback.setState(0);
        synchronized (LOCKED){
            save(aqiFeedback);
            Statistics statistics = new Statistics();
            statistics.setAddress(aqiFeedback.getAddress());
            statistics.setAqiId(aqiFeedback.getEstimatedGrade());
            statistics.setProvinceId(aqiFeedback.getProvinceId());
            statistics.setCityId(aqiFeedback.getCityId());
            statistics.setFdId(aqiFeedback.getTelId());
            statistics.setInformation(feedBackAddDTO.getInformation());
            boolean rs = statisticsService.save(statistics);
            if (rs){
                return 1;
            }else {
                return 0;
            }
        }
    }
//    SqlSessionTemplate

    @Override
    public List<FeedBackListVO> findBackAll(Integer pageSize, Integer lastAfId, String telId) {
        return aqiFeedbackMapper.findBackAll(pageSize,lastAfId,telId);
    }

    @Override
    public List<FeedBackListVO> gmPage(Integer pageSize, Integer lastAfId, String gmCode) {
        return aqiFeedbackMapper.gmPage(pageSize,lastAfId,gmCode);
    }

    @Override
    public FeedBackDetailVO findBackDetail(Integer afId) {
        return aqiFeedbackMapper.feedBackDetail(afId);
    }

    /**
     * 管理员根据条件查询反馈
     * @param adminPageDTO
     * @return
     */
    @Override
    public PageVO<FeedbackShowVO> adminPage(AdminPageDTO adminPageDTO) {
        Integer provinceId = adminPageDTO.getProvinceId();
        Integer cityId = adminPageDTO.getCityId();
        Integer estimatedGrade = adminPageDTO.getEstimatedGrade();
        String afDate = adminPageDTO.getAfDate();
        Integer state = adminPageDTO.getState();
        Integer pageNo = adminPageDTO.getPageNo();
        Integer pageSize = adminPageDTO.getPageSize();
        Integer limitParam1=(pageNo-1)*pageSize;
        Integer limitParam2=pageSize;
        List<FeedbackShowVO> feedbackShowVOS = aqiFeedbackMapper.adminPage(provinceId, cityId, estimatedGrade, afDate, state, limitParam1, limitParam2);
        int count = aqiFeedbackMapper.count(provinceId, cityId, estimatedGrade, afDate, state);
        PageVO<FeedbackShowVO> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(feedbackShowVOS);
        pageVO.setCount(count);
        int pageCount=count/pageSize+(count%pageSize==0?0:1);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public PageVO<AqiDetailVO> aqiPage(AqiPageDTO aqiPageDTO) {
        Integer provinceId = aqiPageDTO.getProvinceId();
        Integer cityId = aqiPageDTO.getCityId();
        String confirmDate = aqiPageDTO.getAfDate();
        Integer pageNo = aqiPageDTO.getPageNo();
        Integer pageSize = aqiPageDTO.getPageSize();
        Integer limitParam1=(pageNo-1)*pageSize;
        Integer limitParam2=pageSize;
        List<AqiDetailVO> aqiDetail = aqiFeedbackMapper.aqiPage(provinceId, cityId, confirmDate, limitParam1, limitParam2);
        int count = aqiFeedbackMapper.aqiCount(provinceId, cityId, confirmDate);
        PageVO<AqiDetailVO> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(aqiDetail);
        pageVO.setCount(count);
        int pageCount=count/pageSize+(count%pageSize==0?0:1);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public FeedBackDetailVO detail(Integer afId) {
        return aqiFeedbackMapper.feedBackDetail(afId);
    }

    @Override
    public AqiDetailVO aqiDetail(Integer afId) {
        return aqiFeedbackMapper.aqiDetail(afId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setGm(Integer afId, Integer gmId) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        String[] s = format.split(" ");
        String assignDate = s[0];
        String assignTime = s[1];
        AqiFeedback aqiFeedback = new AqiFeedback();
        aqiFeedback.setAssignDate(assignDate);
        aqiFeedback.setAssignTime(assignTime);
        aqiFeedback.setState(1);
        LambdaQueryWrapper<AqiFeedback> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AqiFeedback::getAfId,afId)
                .eq(AqiFeedback::getGmId,gmId);
        update(aqiFeedback, wrapper);
        Statistics statistics = new Statistics();
        statistics.setId(afId);
        statistics.setGmId(gmId);
        boolean rs = statisticsService.updateById(statistics);
        if (rs){
            return 1;
        }else {
            return 0;
        }

    }
}
